//
//  HBPhotoBrowserController.swift
//  sinaWeibo
//
//  Created by Beans on 16/7/6.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit
import SVProgressHUD

private let HBPictureBrowserCellReuseIdentifier = "HBPictureBrowserCellReuseIdentifier"

class HBPictureBrowserController: UICollectionViewController {

    // 对外创建一个构造方法, 把数据传递过来
    private var pic_urls: [HBStatusPicViewModel]?
    private var indexPath: NSIndexPath?
    private var flowLayout: UICollectionViewFlowLayout?
    init(collectionViewLayout layout: UICollectionViewFlowLayout, pic_urls: [HBStatusPicViewModel], indexPath: NSIndexPath) {
        super.init(collectionViewLayout: layout)
        self.pic_urls = pic_urls
        self.indexPath = indexPath
        self.flowLayout = layout
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupCollectionView()

        // 跳转到指定索引的图片
        collectionView?.scrollToItemAtIndexPath(indexPath!, atScrollPosition: .Left, animated: true)
    }

    private func setupCollectionView() {
//        collectionView!.backgroundColor = colorWithHexString(hexString: "CCFFCC")
        // Register cell classes
        self.collectionView!.registerClass(HBPictureBrowserCell.self, forCellWithReuseIdentifier: HBPictureBrowserCellReuseIdentifier)
        collectionView?.dataSource = self
        collectionView?.delegate = self

        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false

        flowLayout?.itemSize = collectionView!.bounds.size
        flowLayout?.minimumInteritemSpacing = 0
        flowLayout?.minimumLineSpacing = 0
        flowLayout?.scrollDirection = .Horizontal
    }

    // MARK: UICollectionViewDataSource
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (pic_urls?.count) ?? 0
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HBPictureBrowserCellReuseIdentifier, forIndexPath: indexPath) as! HBPictureBrowserCell
        cell.viewModel = pic_urls![indexPath.item]
        cell.tapToDismissClosure = {
            self.dismissViewControllerAnimated(false, completion: nil)  // 视觉考虑, 关闭modal动画
        }
        cell.longPressToSavePicClosure = {
            // 提示
            SVProgressHUD.showInfoWithStatus("保存成功")
        }

        return cell
    }

    // MARK: UICollectionViewDelegate
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    }

}

